package com.huifaapp.mall.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.InviteCollarNowListViewAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.InviteCollarNow;
import com.huifaapp.mall.bean.SpecialAdvert;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.MyListView;
import com.huifaapp.mall.uitils.UtilsImage;
import com.huifaapp.mall.view.CommomDialog;
import com.huifaapp.mall.view.InviteCollarWin;
import com.huifaapp.mall.view.TakePhotoPopWin;
import com.squareup.okhttp.Request;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 邀请入驻
*
 * Created by Administrator on 2017/12/14.
 */

public class InviteCollarNowActivity extends AppCompatActivity implements View.OnClickListener,InviteCollarWin.OnItemClickListener{
    private TextView tv_collInvite_code;
    private TextView btn_collInvite_copy;
    private TextView tv_invite_yqtuandui;
    private TextView tv_invite_mdmSerweima;
    private TextView tv_invite_collar_jianli;
    InviteCollarWin mPop;
    CommomDialog dialog;
    private String qrcode;
    InviteCollarNow list;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.home_invite_collar_now_a);
        tv_collInvite_code= (TextView) findViewById(R.id.tv_collInvite_code);
        btn_collInvite_copy= (TextView) findViewById(R.id.btn_collInvite_copy);
        btn_collInvite_copy.setOnClickListener(this);
        tv_invite_yqtuandui= (TextView) findViewById(R.id.tv_invite_yqtuandui);
        tv_invite_yqtuandui.setOnClickListener(this);
        tv_invite_mdmSerweima= (TextView) findViewById(R.id.tv_invite_mdmSerweima);
        tv_invite_mdmSerweima.setOnClickListener(this);
        tv_invite_collar_jianli= (TextView) findViewById(R.id.tv_invite_collar_jianli);
        Invitelaod();
        mPop=new InviteCollarWin(this);
        mPop.setOnItemClickListener(this);
        mPop.setFocusable(false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.btn_collInvite_copy:
                ClipboardUtils.copyText(tv_collInvite_code.getText().toString());
                Toast.makeText(InviteCollarNowActivity.this,"复制成功",Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_invite_yqtuandui:
                Invitelaod();
                //设置PopupWindow中的位置T4
                mPop.showAtLocation(InviteCollarNowActivity.this.findViewById(R.id.main_view_invite), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
                break;
            case R.id.tv_invite_mdmSerweima:
                new CommomDialog(InviteCollarNowActivity.this, R.style.dialog, qrcode, new CommomDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean confirm) {
                        if(confirm){
                            Toast.makeText(InviteCollarNowActivity.this,"点击确定", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        }
                    }


                })
                        .setTitle("扫描二维码体验京选友惠APP").show();
                break;


        }
    }
    private void Invitelaod(){
        Log.e("邀请","url=============="+ApiManager.BASE_NIUPAI_URL+"users/invite/yun"+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(InviteCollarNowActivity.this));
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"users/invite/yun"+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(InviteCollarNowActivity.this))
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                            Gson gson = new Gson();
                            list = gson.fromJson(response,InviteCollarNow.class);
                            if(list.getCode().equals("10000")){
                                tv_invite_collar_jianli.setText(list.getData().getTipword());
                                tv_collInvite_code.setText(list.getData().getInvitingCode());
                                qrcode = list.getData().getQrcode();
                                Log.e("tianjing",qrcode);
                            }else if(list.getCode().equals("10001")){
                                Intent intent=new Intent(InviteCollarNowActivity.this,LoginActivity.class);
                                startActivity(intent);
                            }else if(list.getCode().equals("10002")){
                                Intent intent=new Intent(InviteCollarNowActivity.this,LoginActivity.class);
                                startActivity(intent);
                            }else{
                                Toast.makeText(InviteCollarNowActivity.this,list.getMsg(),Toast.LENGTH_SHORT).show();
                            }

                    }
                });
    }


    @Override
    public void setOnItemClick(View v) {
        switch (v.getId()){
            case R.id.tv_shengchenghaibao:
                Intent intent=new Intent(InviteCollarNowActivity.this,PosterActivity.class);
                intent.putExtra("qrcode",qrcode);
                intent.putExtra("invitingimg",list.getData().getInvitingImg());
                startActivity(intent);
                break;
            case R.id.tv_weixinhaoyou:
                UMImage image = new UMImage(InviteCollarNowActivity.this,list.getData().getInvitingImg());//网络图片
                shareUrl("1",null, null,image);
                break;
            case R.id.tv_pengyouquan:
                UMImage image1 = new UMImage(InviteCollarNowActivity.this,list.getData().getInvitingImg());//网络图片
                shareUrl("2",null, null,image1);
                break;
            case R.id.tv_qqhaoyou:
                UMImage imageqq = new UMImage(InviteCollarNowActivity.this,list.getData().getInvitingImg());//网络图片
                shareUrl("3",null, null,imageqq);
                break;
            case R.id.tv_xinlangweibo:
                UMImage imagesl = new UMImage(InviteCollarNowActivity.this,list.getData().getInvitingImg());//网络图片
                shareUrl("4",null, null,imagesl);
                break;
            case R.id.tv_copylianjie:
                ClipboardUtils.copyText(list.getData().getInvitingImg());
                Toast.makeText(InviteCollarNowActivity.this,"复制成功!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_yaoqingcancel:
                mPop.dismiss();
                break;




        }
    }

    /**
     * 分享链接
     */

    private void shareUrl(String type, String con, UMWeb web, UMImage image) {

        android.util.Log.v("shiran","-----------shareUrl()----------");


        UMShareListener umShareListener = new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
//                if (dialog!=null&&!dialog.isShowing()) {
//                    dialog.show();
//                }
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享成功");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享成功");
                } else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享成功");
                } else if(share_media ==SHARE_MEDIA.SINA){
                    ToastUtils.showShortSafe("新浪微博分享成功");
                }else {
                    ToastUtils.showShortSafe("分享成功");

                }
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享失败");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享失败");
                }else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享失败");
                } else if(share_media ==SHARE_MEDIA.SINA) {
                    ToastUtils.showShortSafe("新浪微博分享失败");
                } else {
                    ToastUtils.showShortSafe("分享失败");

                }
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享取消");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享取消");
                } else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享取消");
                } else if(share_media ==SHARE_MEDIA.SINA) {
                    ToastUtils.showShortSafe("新浪微博分享取消");
                } else {
                    ToastUtils.showShortSafe("分享取消");

                }
            }
        };

        if ("1".equals(type)) {  //分享微信
            new ShareAction((Activity) this)
                    .setPlatform(SHARE_MEDIA.WEIXIN).
                    setCallback(umShareListener)
                    .withText(con)
                    .withMedia(image)
                    .share();

        }else  if ("2".equals(type)) { //分享朋友圈

                new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).
                        setCallback(umShareListener)
                        .withMedia(image)
                        .withText(con)
                        .share();

            }else if("3".equals(type)){//分享qq
            new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.QQ).
                    setCallback(umShareListener)
                    .withMedia(image)
                    .withText(con)
                    .share();
        }else if("4".equals(type)){//分享微博
            new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.SINA).
                    setCallback(umShareListener)
                    .withMedia(image)
                    .withText(con)
                    .share();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
